<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
          <el-form-item label="当前余额"  prop="balance">
            <el-input v-model="dataForm.balance" :disabled="true" placeholder="当前余额"></el-input>
          </el-form-item>
          <el-form-item label="充值金额" prop="addBalance">
            <el-input v-model="dataForm.addBalance" placeholder="充值金额"></el-input>
          </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
                  id: '',
                  balance: '',
                  addBalance:''
                },
        dataRule: {
              addBalance: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.agencyminiuser.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                    this.dataForm.mobile = data.user.mobile
                    this.dataForm.name = data.user.name
                    this.dataForm.balance = data.user.balance
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
                  'id': this.dataForm.id,
                  'balance': this.dataForm.addBalance || undefined,
                }
            API.agencyminiuser.addMoney(params).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
